(function($){
    $.fn.Online = function(options){
        var opts = $.extend({}, $.fn.Online.defualts, options);
        $.fn.setList(opts); //调用列表设置
        $.fn.Online.styleType(opts);
        if(opts.DefaultsOpen == false){
            $.fn.Online.closes(opts.Position,0);
        }
        //展开
        $("#OnlineBox > .openTrigger").live("click",function(){$.fn.Online.opens(opts);});
        //关闭
        $("#OnlineBox > .contentBox > .closeTrigger").live("click",function(){$.fn.Online.closes(opts.Position,"fast");});

        //Ie6兼容或滚动方式显示
       /* if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style||opts.Effect==true) {$.fn.Online.scrollType();}
        else if(opts.Effect==false){$("#OnlineBox").css({position:"fixed"});}*/
    }

    //plugin defaults
    $.fn.Online.defualts ={
        Position:"right",//left或right
        Top:160,//顶部距离，默认200px
        Effect:true, //滚动或者固定两种方式，布尔值：true或
        Width:170,//顶部距离，默认200px
        DefaultsOpen:true, //默认展开：true,默认收缩：false
        Style:1,//图标的显示风格，默认显示:1
        Tel:"",//服务热线
        QQList:"",//多个QQ用','隔开，QQ和客服名用'|'隔开
        KFList:""//多个QQ用','隔开，QQ和客服名用'|'隔开
    }

    //展开
    $.fn.Online.opens = function(opts){
        var positionType = opts.Position;
        $("#OnlineBox").css({width:opts.Width+4});
        if(positionType=="left"){$("#OnlineBox > .contentBox").animate({left: 0},"fast");}
        else if(positionType=="right"){$("#OnlineBox > .contentBox").animate({right: 0},"fast");}
        $("#OnlineBox > .openTrigger").hide();
    }

    //关闭
    $.fn.Online.closes = function(positionType,speed){
        $("#OnlineBox > .openTrigger").show();
        var widthValue =$("#OnlineBox > .openTrigger").width();
        var allWidth =(-($("#OnlineBox > .contentBox").width())-6);
        if(positionType=="left"){$("#OnlineBox > .contentBox").animate({left: allWidth},speed);}
        else if(positionType=="right"){$("#OnlineBox > .contentBox").animate({right: allWidth},speed);}
        $("#OnlineBox").animate({width:widthValue},speed);

    }

    //风格选择
    $.fn.Online.styleType = function(opts){
        var typeNum = 1;
        switch(opts.Style)
            { case 1:
                typeNum = 41;
	     break
            case 2:
                typeNum = 42;
	     break
            case 3:
                typeNum = 44;
	     break
            case 4:
                typeNum = 45;
	     break
            case 5:
                typeNum = 46;
	     break
            case 6:
                typeNum = 47;
	     break
	     default:
                typeNum = 41;
	   }
                return typeNum;
        }

        //子插件：设置列表参数
        $.fn.setList = function(opts,opts1){
            $("#customer_service").append("<div class='OnlineBox' id='OnlineBox' style='top:-600px; position:absolute;'><div class='openTrigger' style='display:none' title='展开'></div><div class='contentBox'><div class='closeTrigger' title='关闭'></div><div class='titleBox'><span>客服中心</span></div><div class='listBox'></div><div class='listBos'>" +
                $.fn.Online.splitStr(opts, 'KFList')
                //"</div><div class='listBos'><div class='KFList'><img src='include/images/weixin.jpg' width='80' height='80' style='margin-left:45px;'/></div>" +
                //"</div><div class='tels'><font>服务热线：</font><span>"+opts.Tel+"</span></div></div></div>"
                );
            if(opts.Tel==""){$(".tels").remove();}
            $("#OnlineBox > .contentBox").width(opts.Width)
            if(opts.QQList==""){$("#OnlineBox > .contentBox > .listBox").append("<p style='padding:15px'>暂无在线销售。</p>")}
            else{var qqListHtml = $.fn.Online.splitStr(opts, 'QQList');$("#OnlineBox > .contentBox > .listBox").append(qqListHtml);	}
            if(opts.Position=="left"){$("#OnlineBox").css({left:0});}
            else if(opts.Position=="right"){$("#OnlineBox").css({right:0})}
            $("#OnlineBox").css({top:opts.Top,width:opts.Width+4});
            var allHeights=0;
            if($("#OnlineBox > .contentBox").height() < $("#OnlineBox > .openTrigger").height()){
                allHeights = $("#OnlineBox > .openTrigger").height()+4;
            } else{allHeights = $("#OnlineBox > .contentBox").height()+40;}
            $("#OnlineBox").height(allHeights);
            if(opts.Position=="left"){$("#OnlineBox > .openTrigger").css({left:0});}
            else if(opts.Position=="right"){$("#OnlineBox > .openTrigger").css({right:0});}
        }

        //滑动式效果
        $.fn.Online.scrollType = function(){
            $("#OnlineBox").css({position:"absolute"});
            var topNum = parseInt($("#OnlineBox").css("top")+"");
            $(window).scroll(function(){
                var scrollTopNum = $(window).scrollTop();//获取网页被卷去的高
                $("#OnlineBox").stop(true,false).delay(200).animate({top:scrollTopNum+topNum},"slow");
            });
        }

        //分割QQ
        $.fn.Online.splitStr = function(opts, type){

            var strs= new Array(); //定义一数组
            var text = opts[type];
            strs=text.split(","); //字符分割
            var QqHtml=""
            for (var i=0;i<strs.length;i++){
                var subStrs= new Array(); //定义一数组
                var subQqlist = strs[i];
                subStrs = subQqlist.split("|"); //字符分割
                QqHtml = QqHtml+"<div class='" + type + "'><span>"+subStrs[1]+"：</span><div class='ico'><a target='_blank' href='http://wpa.qq.com/msgrd?v=3&uin="+subStrs[0]+"&site=qq&menu=yes'><img border='0' src='http://wpa.qq.com/pa?p=2:"+subStrs[0]+":"+$.fn.Online.styleType(opts)+" &amp;r=0.22914223582483828' alt='点击这里'></a></div><div style='clear:both;'></div></div>";
            }
            return QqHtml;
        }
})(jQuery);